//
//  PostGoodsViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/8/13.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "WebViewWithStringVC.h"
#import <WebKit/WebKit.h>

@interface WebViewWithStringVC ()<WKNavigationDelegate,WKUIDelegate>

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation WebViewWithStringVC

- (void)viewDidLoad {
    [super viewDidLoad];
	self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
	[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.htmlAddress]]];
    NSLog(@"the address with token is %@", self.htmlAddress);
	self.webView.navigationDelegate = self;
	self.webView.UIDelegate = self;
	//开了支持滑动返回
	self.webView.allowsBackForwardNavigationGestures = YES;
    [self.view addSubview:self.webView];
	
    //填充顶部间隙
    UIView *fillTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    fillTop.backgroundColor = UIMainColorRGB;
    [self.view addSubview:fillTop];
    
    //取消按钮
	UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.webView addSubview:cancelBtn];
	[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
	cancelBtn.titleLabel.font = kUHSystemFontWithSize(14);
	[cancelBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
	cancelBtn.frame = CGRectMake(SW(8), 22, 50, 44);
	[cancelBtn addTarget:self action:@selector(cancelPostClick:) forControlEvents:UIControlEventTouchUpInside];
	
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - webViewDelegate
/// 是否允许加载网页，也可获取js要打开的url，通过截取此url可与js交互
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	
	NSString * requestString = [[request URL] absoluteString];
	requestString = [requestString stringByRemovingPercentEncoding];
	//获取H5页面里面按钮的操作方法,根据这个进行判断返回是内部的还是push的上一级页面
	if ([requestString hasPrefix:@"goback:"]) {
		//[self.navigationController popViewControllerAnimated:YES];
		[self dismissViewControllerAnimated:YES completion:nil];
		
	}else{
		[self.webView goBack];
	}
	return YES;
}
/// 开始加载网页
- (void)webViewDidStartLoad:(UIWebView *)webView {
	NSURLRequest *request = webView.request;
	NSLog(@"webViewDidStartLoad-url=%@--%@",[request URL],[request HTTPBody]);
}
/// 网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
	NSURLRequest *request = webView.request;
	NSURL *url = [request URL];
	if ([url.path isEqualToString:@"/normal.html"]) {
		NSLog(@"isEqualToString");
	}
	NSLog(@"webViewDidFinishLoad-url=%@--%@",[request URL],[request HTTPBody]);
	//NSLog(@"%@",[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]);
}
/// 网页加载错误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
	NSURLRequest *request = webView.request;
	NSLog(@"didFailLoadWithError-url=%@--%@",[request URL],[request HTTPBody]);
	
}


- (void)cancelPostClick:(UIButton *)sender {
	[self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
